package com.starview.platform.resource;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.InputStream;
import java.io.Reader;

import com.starview.platform.data.value.ResourceURI;

public class FileTransducer implements Transducer
{
  private static String fileSeparator = System.getProperty("file.separator");

  @Override
  public Reader getReader(ResourceURI uri) throws ResourceException
  {
    File file = new File(uri.getPath().replace("/", fileSeparator));
    if (file.canRead())
      try {
        return new FileReader(file);
      } catch (FileNotFoundException e) {
        throw new ResourceException(file + " not accessable");
      }
    else
      throw new ResourceException(file + " not accessable");
  }

  @Override
  public InputStream getInputStream(ResourceURI uri) throws ResourceException
  {
    File file = new File(uri.getPath().replace("/", fileSeparator));
    if (file.canRead())
      try {
        return new FileInputStream(file);
      } catch (FileNotFoundException e) {
        throw new ResourceException(file + " not accessable");
      }
    else
      throw new ResourceException(file + " not accessable");
  }

  @Override
  public boolean exists(ResourceURI uri) throws ResourceException
  {
    File file = new File(uri.getPath());
    return file.exists();
  }

  @Override
  public void putResource(ResourceURI uri, Object resource) throws ResourceException
  {
    throw new ResourceException("not implemented");
  }
}